package config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate相关类在spring-web依赖里
 */
@Configuration
@ComponentScan("com.elasticsearchtemplate")
public class ElasticsearchRestTemplateConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http")));
        return client;
    }

    /**
     * ElasticsearchTemplate已经废弃，建议用ElasticsearchRestTemplate
     * 创建ElasticsearchRestTemplate需要RestHighLevelClient对象
     *
     * @return
     */
    @Bean
    public ElasticsearchRestTemplate abc(){
        ElasticsearchRestTemplate template = new ElasticsearchRestTemplate(restHighLevelClient());
        return template;
    }
}
